<!DOCTYPE html>
<html>
<!--
Copyright 2008 The Closure Library Authors. All Rights Reserved.

Use of this source code is governed by the Apache License, Version 2.0.
See the COPYING file for details.
-->
<head>
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Closure Unit Tests - goog.structs.InversionMap</title>
<script type="text/javascript" src="../base.js"></script>
<script type="text/javascript">
  goog.require('goog.structs.InversionMap');
  goog.require('goog.testing.jsunit');
</script>
</head>
<body>
<script type="text/javascript">
  function testInversionWithDelta() {
    var alphabetNames = new goog.structs.InversionMap(
        [ 0, 97, 1, 1, 1, 20, 1, 1, 1 ],
        [ null,
          'LATIN SMALL LETTER A',
          'LATIN SMALL LETTER B',
          'LATIN SMALL LETTER C',
          null,
          'LATIN SMALL LETTER X',
          'LATIN SMALL LETTER Y',
          'LATIN SMALL LETTER Z',
          null ],
        true);

    assertEquals('LATIN SMALL LETTER A', alphabetNames.at(97));
    assertEquals('LATIN SMALL LETTER Y', alphabetNames.at(121));
    assertEquals(null, alphabetNames.at(140));
    assertEquals(null, alphabetNames.at(0));
  }

  function testInversionWithoutDelta() {
    var alphabetNames = new goog.structs.InversionMap(
        [ 0, 97, 98, 99, 100, 120, 121, 122, 123 ],
        [ null,
          'LATIN SMALL LETTER A',
          'LATIN SMALL LETTER B',
          'LATIN SMALL LETTER C',
          null,
          'LATIN SMALL LETTER X',
          'LATIN SMALL LETTER Y',
          'LATIN SMALL LETTER Z',
          null ],
        false);

    assertEquals('LATIN SMALL LETTER A', alphabetNames.at(97));
    assertEquals('LATIN SMALL LETTER Y', alphabetNames.at(121));
    assertEquals(null, alphabetNames.at(140));
    assertEquals(null, alphabetNames.at(0));
  }

  function testInversionWithoutDeltaNoOpt() {
    var alphabetNames = new goog.structs.InversionMap(
        [ 0, 97, 98, 99, 100, 120, 121, 122, 123 ],
        [ null,
          'LATIN SMALL LETTER A',
          'LATIN SMALL LETTER B',
          'LATIN SMALL LETTER C',
          null,
          'LATIN SMALL LETTER X',
          'LATIN SMALL LETTER Y',
          'LATIN SMALL LETTER Z',
          null ]);

    assertEquals('LATIN SMALL LETTER A', alphabetNames.at(97));
    assertEquals('LATIN SMALL LETTER Y', alphabetNames.at(121));
    assertEquals(null, alphabetNames.at(140));
    assertEquals(null, alphabetNames.at(0));
  }

  function testInversionMapSplice1() {
    var alphabetNames = newAsciiMap();
    alphabetNames.spliceInversion(
        [ 99, 105, 114 ], [ 'XXX', 'YYY', 'ZZZ' ]);
    assertEquals('LATIN SMALL LETTER B', alphabetNames.at(98));
    assertEquals('XXX', alphabetNames.at(100));
    assertEquals('ZZZ', alphabetNames.at(114));
    assertEquals('ZZZ', alphabetNames.at(119));
    assertEquals('LATIN SMALL LETTER X', alphabetNames.at(120));
  }

  function testInversionMapSplice2() {
    var alphabetNames = newAsciiMap();
    alphabetNames.spliceInversion(
        [ 105, 114, 121 ], [ 'XXX', 'YYY', 'ZZZ' ]);
    assertEquals(null, alphabetNames.at(104));
    assertEquals('XXX', alphabetNames.at(105));
    assertEquals('YYY', alphabetNames.at(120));
    assertEquals('ZZZ', alphabetNames.at(121));
    assertEquals('LATIN SMALL LETTER Z', alphabetNames.at(122));
  }

  function testInversionMapSplice3() {
    var alphabetNames = newAsciiMap();
    alphabetNames.spliceInversion(
        [ 98, 99 ], [ 'CHANGED LETTER B', 'CHANGED LETTER C' ]);
    assertEquals('LATIN SMALL LETTER A', alphabetNames.at(97));
    assertEquals('CHANGED LETTER B', alphabetNames.at(98));
    assertEquals('CHANGED LETTER C', alphabetNames.at(99));
    assertEquals('LATIN SMALL LETTER D', alphabetNames.at(100));
    assertEquals(null, alphabetNames.at(101));
  }

  function testInversionMapSplice4() {
    var alphabetNames = newAsciiMap();
    alphabetNames.spliceInversion(
        [ 98, 1 ], [ 'CHANGED LETTER B', 'CHANGED LETTER C' ],
        true /* delta mode */);
    assertEquals('LATIN SMALL LETTER A', alphabetNames.at(97));
    assertEquals('CHANGED LETTER B', alphabetNames.at(98));
    assertEquals('CHANGED LETTER C', alphabetNames.at(99));
    assertEquals('LATIN SMALL LETTER D', alphabetNames.at(100));
    assertEquals(null, alphabetNames.at(101));
  }

  function testInversionMapSplice5() {
    var map = new goog.structs.InversionMap(
        [0, 97, 98, 99],
        [null,
         'LATIN SMALL LETTER A',
         'LATIN SMALL LETTER B',
         'LATIN SMALL LETTER C']);
    map.spliceInversion(
        [98], ['CHANGED LETTER B']);
    assertEquals('LATIN SMALL LETTER A', map.at(97));
    assertEquals('CHANGED LETTER B', map.at(98));
    assertEquals('LATIN SMALL LETTER C', map.at(99));

    assertArrayEquals([0, 97, 98, 99], map.rangeArray);
  }

  function newAsciiMap() {
    return new goog.structs.InversionMap(
        [ 0, 97, 98, 99, 100, 101, 120, 121, 122, 123 ],
        [ null,
          'LATIN SMALL LETTER A',
          'LATIN SMALL LETTER B',
          'LATIN SMALL LETTER C',
          'LATIN SMALL LETTER D',
          null,
          'LATIN SMALL LETTER X',
          'LATIN SMALL LETTER Y',
          'LATIN SMALL LETTER Z',
          null ]);
  }

</script>
</body>
</html>
